<?php namespace web\home\controller;
/**
 * Created by PhpStorm.
 * User: qianchenmiao
 * Date: 2016/10/12
 * Time: 23:34
 * 前台Common 所有页面继承Common 从而获取公共数据
 */
 class Common{
	 public function __construct()
	 {
//		 获得品牌表里边的数据
		 $brand=Db::table('brand')->lists('bid,bname');
		 //获得分类表的数据
		 $categoryData=Db::table('category')->get();
		 $categoryData=Data::channelLevel($categoryData, $pid = 0, $html = "&nbsp;", $fieldPri = 'cid', $fieldPid = 'pid');
//		 推荐商品获取
//获取方式不理想每个子分类至少获取一条数据
		 foreach($categoryData as $k=>$v){
			foreach($v['_data']as $kk=>$vv){
				$categoryData[$k]['tuijian'][]=Db::table('goods')
				->where('cid',$vv['cid'])->orderBy('click','desc')->first();
			}

		}
		 foreach($categoryData as $k=>$v) {
			 foreach ($categoryData[$k]['tuijian'] as $kk=> $value) {

				 if ($categoryData[$k]['tuijian'][$kk]) {
					 $categoryData[$k]['tuijian'][$kk] = $value;
				 } else {
					 unset($categoryData[$k]['tuijian'][$kk]);
				 }
			 }
		 }
		 if($_SESSION['cart']){
			 foreach ($_SESSION['cart'] as$k=>$v){
				 $CgoodsData[$k]=$v;
			 }
			 foreach($CgoodsData['goods'] as $k=>$v){
				 if($v){
					 $CgoodsData['goods'][$k]=$v;
					 $CgoodsData['goods'][$k]['pic']=Db::table('goods')->where('gid',$v['id'])->pluck('pic');
				 }
			 }
		 }
//		 p($CgoodsData);
		 View::with('categoryData',$categoryData)->with('brand',$brand)->with('CgoodsData',$CgoodsData);

	 }

 }